"""
现货数据采集任务
"""
from celery import shared_task
from modules.data_collector.spot_collector import SpotCollector
import logging

logger = logging.getLogger(__name__)

@shared_task(bind=True)
def collect_spot_info_task(self, exchange="上海黄金交易所"):
    """采集现货合约信息任务"""
    try:
        collector = SpotCollector()
        new_count, update_count = collector.collect_spot_info(exchange=exchange)
        return {
            'new_count': new_count,
            'update_count': update_count,
            'exchange': exchange
        }
    except Exception as e:
        logger.error(f"现货合约信息采集任务失败: {str(e)}")
        raise self.retry(exc=e, countdown=60)

@shared_task(bind=True)
def collect_spot_daily_task(self, symbol, start_date=None, end_date=None):
    """采集现货日线数据任务"""
    try:
        collector = SpotCollector()
        inserted_count = collector.collect_spot_daily(
            symbol=symbol,
            start_date=start_date,
            end_date=end_date,
            task_id=self.request.id
        )
        return {
            'inserted_count': inserted_count,
            'symbol': symbol
        }
    except Exception as e:
        logger.error(f"现货日线数据采集任务失败: {str(e)}")
        raise self.retry(exc=e, countdown=60)